﻿Imports TNCDAL
Imports System.Data.Linq
Imports System.Linq


Public Class CAccessory
    Dim db As New TanNamChinhDataContext

    Public Function SelectAllRecord() As IQueryable(Of tblAccessory)
        Dim ac = From p In db.tblAccessories
                 Where p.IsActive = True
                Select p
                Order By p.AccessoryName

        Return ac
    End Function

    Public Function SelectAllRecordByType(ByVal Type As Integer) As IQueryable(Of tblAccessory)
        Dim ac = From p In db.tblAccessories
                 Where p.IsActive = True And p.Type = Type
                Select p
                Order By p.AccessoryName

        Return ac
    End Function

    Public Function SelectAllRecordByTypeAndName(ByVal Type As Integer, ByVal Name As String) As IQueryable(Of tblAccessory)
        Dim ac = From p In db.tblAccessories
                 Where p.IsActive = True And p.Type = Type And p.AccessoryName.ToLower.Contains(Name.ToLower)
                Select p
                Order By p.AccessoryName

        Return ac
    End Function

    Public Function SelectRecordById(ByVal id As Integer) As tblAccessory
        Dim ac = (From p In db.tblAccessories
                 Where p.ID_Accessory = id
                 Select p).Single
        Return ac

    End Function

    Public Sub Insert(ByVal obj As tblAccessory)
        db.tblAccessories.InsertOnSubmit(obj)
        db.SubmitChanges()
    End Sub

    Public Sub Update(ByVal obj As tblAccessory)
        Dim ac = SelectRecordById(obj.ID_Accessory)
        ac.AccessoryName = obj.AccessoryName
        db.SubmitChanges()
    End Sub

    Public Sub Delete(ByVal id As Integer)
        Dim ac = SelectRecordById(id)
        ac.IsActive = False
        db.SubmitChanges()
        'Dim ac = SelectRecordById(id)
        'db.tblAccessories.DeleteOnSubmit(ac)
        'db.SubmitChanges()
    End Sub
End Class

